"use strict";

goog.require("Blockly.Blocks");
goog.require("Blockly");
goog.require("Blockly.JavaScript");

Blockly.Blocks["Move_Forward"] = {
  init: function() {
    this.jsonInit({
      message0: "向前运动 %1",
      args0: [
        {
          type: "input_value",
          name: "TEXT"
        }
      ],
      previousStatement: null,
      nextStatement: null,
      style: "text_blocks",
      tooltip: Blockly.Msg["TEXT_PRINT_TOOLTIP"],
      helpUrl: Blockly.Msg["TEXT_PRINT_HELPURL"]
    });
  }
};

Blockly.JavaScript["Move_Forward"] = function(block) {
  // Print statement.
  var msg =
    Blockly.JavaScript.valueToCode(
      block,
      "TEXT",
      Blockly.JavaScript.ORDER_NONE
    ) || "''";
  return "moveForward(" + msg + ");\n";
};

Blockly.Blocks["Move_Down"] = {
  init: function() {
    this.jsonInit({
      message0: "向下运动 %1",
      args0: [
        {
          type: "input_value",
          name: "TEXT"
        }
      ],
      previousStatement: null,
      nextStatement: null,
      style: "text_blocks",
      tooltip: Blockly.Msg["TEXT_PRINT_TOOLTIP"],
      helpUrl: Blockly.Msg["TEXT_PRINT_HELPURL"]
    });
  }
};

Blockly.JavaScript["Move_Down"] = function(block) {
  // Print statement.
  var msg =
    Blockly.JavaScript.valueToCode(
      block,
      "TEXT",
      Blockly.JavaScript.ORDER_NONE
    ) || "''";
  return "moveDown(" + msg + ");\n";
};

Blockly.Blocks["Print_Str"] = {
  init: function() {
    this.jsonInit({
      message0: "打印不换行 %1",
      args0: [
        {
          type: "input_value",
          name: "TEXT"
        }
      ],
      previousStatement: null,
      nextStatement: null,
      style: "text_blocks",
      tooltip: Blockly.Msg["TEXT_PRINT_TOOLTIP"],
      helpUrl: Blockly.Msg["TEXT_PRINT_HELPURL"]
    });
  }
};

Blockly.JavaScript["Print_Str"] = function(block) {
  // Print statement.
  var msg =
    Blockly.JavaScript.valueToCode(
      block,
      "TEXT",
      Blockly.JavaScript.ORDER_NONE
    ) || "''";
  return "PrintStr(" + msg + ");\n";
};

Blockly.Blocks["Println_Str"] = {
  init: function() {
    this.jsonInit({
      message0: "打印换行 %1",
      args0: [
        {
          type: "input_value",
          name: "TEXT"
        }
      ],
      previousStatement: null,
      nextStatement: null,
      style: "text_blocks",
      tooltip: Blockly.Msg["TEXT_PRINT_TOOLTIP"],
      helpUrl: Blockly.Msg["TEXT_PRINT_HELPURL"]
    });
  }
};
Blockly.JavaScript["Println_Str"] = function(block) {
  // Print statement.
  var msg =
    Blockly.JavaScript.valueToCode(
      block,
      "TEXT",
      Blockly.JavaScript.ORDER_NONE
    ) || "''";
  return "PrintlnStr(" + msg + ");\n";
};

function moveForward(step) {
  imgLeft = img.offsetLeft;
  img.style.left = imgLeft + step * 5 + "px";
  return true;
}

function moveDown(step) {
  imgTop = img.offsetTop;
  img.style.top = imgTop + step * 5 + "px";
  return true;
}
function PrintStr(step) {
  creadiv(step);
  return true;
}
function PrintlnStr(step) {
  println(step);
  return true;
}
